<?php
/* FIRST TUESDAY FEEDS WIDGET */

class FT_Feeds_Widget extends WP_Widget {

	// Constructor del widget
	function FT_Feeds_Widget() {
		// Informacion basica del widget
		$datos_widget = array('classname' => 'ft_feeds_widget', 'description' => 'Widget que muestra feeds (RSS) con imágenes y textos.' );
		$this->WP_Widget($datos_widget['classname'], 'RSS Feeds First Tuesday', $datos_widget);
	}

	// Muestra el widget
	function widget($args, $instance) {
		extract($args);

		// Definicion del titulo del widget
		$titulo = $instance['title'];
		$feeds = $instance['feeds'];
		$feeds_max = $instance['feeds_max'];
		$target = $instance['target'];
		$images = $instance['images'];
		$text_title = $instance['text-title'];
		$text_content = $instance['text-content'];
		
		echo $before_widget;
		echo $before_title.$titulo.$after_title;

		if(!empty($feeds)) :
			
			if($images && !$text_title) $class = 'imagenes';
			if($text_title && !$images) $class = 'titulos';
			if($images && $text_title) $class = 'imagenes-titulos';
			//if($text_content) $class .= ' contenidos';
	?>
			<ul id="feeds-list" class="item-list <?php echo $class; ?>">
				<?php
					// Obtiene los itemes del feed
					$rss_items = $this->FT_Obtener_Info_Feed($feeds, $feeds_max);
	
					// Loop a traves de cada item del feed.
					if(!empty($rss_items)) :
						foreach($rss_items as $item) :
				?>
							<li class="item">
								<?php
									if($images) :
										// Obtiene la imagen del item
										$imagen_src = '';
										$imagen = '';
					
										if($item_imagen = $item->get_item_tags(SIMPLEPIE_NAMESPACE_MEDIARSS, 'thumbnail')) :
											$imagen_src = $item_imagen[0]['attribs']['']['url'];
											$imagen = '<img src="'.$imagen_src.'" alt="'.$item->get_title().'"/>';
										elseif($item_imagen = $item->get_enclosure()) :
											$enclosures = $item->get_enclosures();
											$imagen_src = $enclosures[0]->get_link();
											$imagen = '<img src="'.$imagen_src.'" alt="'.$item->get_title().'"/>';
										else :
											preg_match_all('/<img[^>]+>/i', $item->get_content(), $imagenes);
											if($imagenes)
												$imagen = $imagenes[0][0];
										endif;
										if(!empty($imagen)) :
								?>
											<div class="item-imagen">
												<a href="<?php echo $item->get_permalink(); ?>" title="<?php echo $item->get_title().' - '.__('Publicado el', 'firsttuesday').' '.$item->get_date('d F Y, H:i'); ?>" target="<?php echo $target; ?>">
													<?php echo $imagen; ?>
												</a>
											</div>
								<?php
										endif;
									endif;
	
									if($text_title || $text_content) :
								?>
										<div class="item-info">
											<?php
												if($text_title) :
											?>
													<div class="item-titulo">
														<a href="<?php echo $item->get_permalink(); ?>" title="<?php echo $item->get_title(); ?>" target="<?php echo $target; ?>">
															<?php echo $item->get_title(); ?>
														</a>
													</div>
													<div class="item-campos">
														<?php echo $item->get_date('d-m-Y, H:i'); ?>
													</div>
											<?php
												endif;

												if($text_content && $item_descripcion = $item->get_enclosure()) :
											?>
													<div class="item-campos-especial">
														<?php echo $item_descripcion->get_description(); ?>
													</div>
											<?php
												endif;
											?>
										</div>
								<?php
									endif;
								?>
								<div class="clear"></div>
							</li>
				<?php
						endforeach;
					else :
				?>
						<li class="widget-error">
							<?php _e('No hay contenidos.', 'firsttuesday'); ?>
						</li>
				<?php
					endif;
				?>
			</ul>
			<div style="clear:both;"></div>
	<?php
		endif;

		echo $after_widget;
	}

	// Actualiza la configuracion del widget
	function update($new_instance, $old_instance) {
		$instance = $old_instance;
		$instance['title'] = strip_tags($new_instance['title']);
		$instance['feeds'] = strip_tags($new_instance['feeds']);
		$instance['feeds_max'] = strip_tags($new_instance['feeds_max']);
		$instance['target'] = strip_tags($new_instance['target']);
		$instance['images'] = strip_tags($new_instance['images']);
		$instance['text-title'] = strip_tags($new_instance['text-title']);
		$instance['text-content'] = strip_tags($new_instance['text-content']);

		return $instance;

	}

	// Muetra el formulario de administracion del widget
	function form($instance) {
		$instance = wp_parse_args((array) $instance, array('title' => '', 'feeds' => '','feeds_max' => 3,'target' => '_blank', 'images' => 'images', 'text-title' => 'text-title', 'text-content' => ''));
		$title = strip_tags($instance['title']);
		$feeds = strip_tags($instance['feeds']);
		$feeds_max = strip_tags($instance['feeds_max']);
		$target = strip_tags($instance['target']);
		$images = strip_tags($instance['images']);
		$text_title = strip_tags($instance['text-title']);
		$instance['text-content'] = strip_tags($instance['text-content']);
	?>
		<p>
			<label for="<?php echo $this->get_field_id('title'); ?>">
				Título: <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo attribute_escape($title); ?>" />
			</label>
		</p>
		<p>
			<label for="<?php echo $this->get_field_id('feeds__title'); ?>">
				RSS feeds (separados por comas): <input class="widefat" id="<?php echo $this->get_field_id('feeds'); ?>" name="<?php echo $this->get_field_name('feeds'); ?>" type="text" value="<?php echo attribute_escape($feeds); ?>" />
			</label>
		</p>
		<p>
			<label for="<?php echo $this->get_field_id('feeds_max'); ?>">Número de itemes a mostrar: <input class="widefat" id="<?php echo $this->get_field_id('feeds_max'); ?>" name="<?php echo $this->get_field_name('feeds_max'); ?>" type="text" value="<?php echo attribute_escape($feeds_max); ?>" />
		</label>
		</p>
		<p>
			<label for="<?php echo $this->get_field_id('target'); ?>_a">
				Dónde abrir los enlaces:<br/>
				<?php
					echo '<input type="radio" name="'.$this->get_field_name('target').'" id="'.$this->get_field_id('target').'_a" ';
					if($instance['target'] == '_self')
						echo 'checked ';
					echo 'value="_self"/> En la misma ventana';
				?>
			</label><br/>
			<label for="<?php echo $this->get_field_id('target'); ?>_b">
				<?php
					echo '<input type="radio" name="'.$this->get_field_name('target').'" id="'.$this->get_field_id('target').'_b" ';
					if($instance['target'] == '_blank' || empty($instance['target']))
						echo 'checked ';
					echo 'value="_blank"/> En una nueva ventana';
				?>
			</label>
		</p>
		<p>
			Contenidos:<br/>
			<label for="<?php echo $this->get_field_id('images'); ?>">
				<?php 
					echo '<input type="checkbox" name="'.$this->get_field_name('images').'" id="'.$this->get_field_id('images').'" ';
					if($instance['images'] == 'images' || (empty($instance['images']) && empty($instance['text-title'])))
						echo 'checked ';
					echo 'value="images"/> Mostrar las imágenes';
				?>
			</label><br/>
			<label for="<?php echo $this->get_field_id('text-title'); ?>">
				<?php
					echo '<input type="checkbox" name="'.$this->get_field_name('text-title').'" id="'.$this->get_field_id('text-title').'" ';
					if($instance['text-title'] == 'text-title' || (empty($instance['images']) && empty($instance['text-title'])))
						echo 'checked ';
					echo 'value="text-title"/> Mostrar los títulos';
				?>
			</label><!--<br/>
			<label for="<?php echo $this->get_field_id('text-content'); ?>">
				<?php
					echo '<input type="checkbox" name="'.$this->get_field_name('text-content').'" id="'.$this->get_field_id('text-content').'" ';
					if($instance['text-content'] == 'text-content')
						echo 'checked ';
					echo 'value="text-content"/> Mostrar el contenido';
				?>
			</label>-->
		</p>
	<?php
	}
	
	/* Obtiene la informacion de feeds */
	
	function FT_Obtener_Info_Feed($feeds, $feeds_max) {
	
		// Obtener base RSS Feed(s)
		include_once(ABSPATH . WPINC . '/feed.php');
	
		// Obtener Feed(s) desde los origines indicados
		$feeds = split(',', $feeds);
		$rss = fetch_feed($feeds);
	
		// Determinar cuantos itemes existen
		$items_max = $rss->get_item_quantity((int)$feeds_max);
	
		return $rss->get_items(0, $items_max);
	}
}
?>